package com.gr.dmbackend.service.Impl.sysadmin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.dmbackend.mapper.DormitoryMapper;
import com.gr.dmbackend.model.Dormitory;
import com.gr.dmbackend.service.sysadmin.GetDormIdListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GetDormIdListServiceImpl implements GetDormIdListService {

    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public JSONObject getDormIdList(Map<String, String> data) {

        JSONObject resp = new JSONObject();

        String building = data.get("building");
        String floor = data.get("floor");

        if (building.isEmpty() || floor.isEmpty()) {
            resp.put("error_message", "楼栋或楼层为空");
            return resp;
        }

        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("floor", floor)
                .eq("building", building);
        List<Dormitory> dorms = dormitoryMapper.selectList(queryWrapper);

        if (dorms.isEmpty()) {
            resp.put("error_message", "未找到相应寝室号");
            return resp;
        }

        resp.put("dorms", dorms);
        resp.put("error_message", "success");

        return resp;
    }
}
